package com.biao.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLIntegrityConstraintViolationException;
//全局异常处理器,其参数是配置此注解的扫描范围,
// annotations： 指定一个或多个注解，被这些注解所标记的 Controller 会被该 @ControllerAdvice 管理
//https://blog.csdn.net/user2025/article/details/105458842
@RestControllerAdvice(annotations = {RestController.class, Controller.class})
@Slf4j
public class GlobalExceptionHandler {
    //违反唯一约束
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public ResponseResult exception(SQLIntegrityConstraintViolationException e) {
        log.info(e.getMessage());
        if (e.getMessage().contains("Duplicate entry")) {
            String[] s = e.getMessage().split(" ");
            return ResponseResult.error(s[2] + "已存在");
        }
        return ResponseResult.error("失败");
    }

    @ExceptionHandler(CustoException.class)
    public ResponseResult exception(CustoException e) {

        return ResponseResult.error(e.getMessage());
    }
}
